﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLZJFurnitureSCM.BLL;
using QLZJFurnitureSCM.Model;
using System.Text.RegularExpressions;

namespace QLZJFurnitureSCM.WinForm
{
    public partial class FrmRecordView : Form
    {
        private List<Purchase> lstPurchase = null;

        public FrmRecordView()
        {
            InitializeComponent();
        }

        private delegate void DrawDataGridViewDelegate();

        private void DrawDataGridView()
        {
            lstPurchase = new PurchaseService().FindAll();
            this.dataGridViewRecord.DataSource = lstPurchase;
            SetDgvHeaderText();
            this.cboSearchType.SelectedIndex = 0;
        }

        private void FrmRecordView_Load(object sender, EventArgs e)
        {
            this.Invoke(new DrawDataGridViewDelegate(DrawDataGridView));
            
            // TODO: This line of code loads data into the 'furnitureDataSet.Purchase' table. You can move, or remove it, as needed.
            //this.purchaseTableAdapter.Fill(this.furnitureDataSet.Purchase);

            
            /*
            int columnIndex = this.dataGridViewRecord.Columns["colPurchaseTotalPrice"].Index;
            this.dataGridViewRecord.Columns[columnIndex].ValueType = typeof(decimal);
            for(int i=0; i< dataGridViewRecord.Rows.Count; i++){
                this.dataGridViewRecord[columnIndex, i].Value = decimal.Parse(this.dataGridViewRecord["purchasePriceDataGridViewTextBoxColumn", i].Value.ToString()) * int.Parse(this.dataGridViewRecord["purchaseCountDataGridViewTextBoxColumn", i].Value.ToString());
            }
            */
        }

        private void SetDgvHeaderText()
        {
            DataGridViewCellStyle priceCellStyle = new DataGridViewCellStyle();
            priceCellStyle.Format = "C1";
            priceCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dataGridViewRecord.Columns["purchaseId"].Visible = false;
            dataGridViewRecord.Columns["purchaseDate"].HeaderText = "进货日期";
            dataGridViewRecord.Columns["purchaseProductName"].HeaderText = "产品名称";
            dataGridViewRecord.Columns["purchaseProductType"].HeaderText = "产品类别";
            dataGridViewRecord.Columns["purchasePrice"].HeaderText = "单价";
            dataGridViewRecord.Columns["purchasePrice"].DefaultCellStyle = priceCellStyle;
            dataGridViewRecord.Columns["purchaseCount"].HeaderText = "数量";
            dataGridViewRecord.Columns["purchaseTotalPrice"].HeaderText = "合计";
            dataGridViewRecord.Columns["purchaseTotalPrice"].DefaultCellStyle = priceCellStyle;
            dataGridViewRecord.Columns["purchaseByUser"].HeaderText = "管理员";
            decimal sum = (from t in lstPurchase select t.PurchaseTotalPrice).Sum();
            string sumChs = ConvertToChinese(sum);
            this.toolStripStatusLabelFrmRecordView.Text = string.Format("历史统计得:进货 {0} 次, 商品 {1} 件, 资金: {2} 元 合: {3}.", lstPurchase.Count, (from t in lstPurchase select t.PurchaseCount).Sum(), sum.ToString("C1"), sumChs);
        }

        private string ConvertToChinese(decimal x)
        {
            string s = x.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");
            string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");
            return Regex.Replace(d, ".", delegate(Match m) { return "负元空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟萬億兆京垓秭穰"[m.Value[0] - '-'].ToString(); });
        }

        private void TotalMoney(List<Purchase> lstPurchaseNeedCount)
        {
            
        }

        private void dataGridViewRecord_Paint(object sender, PaintEventArgs e)
        {
            for (int i = 0; i < this.dataGridViewRecord.Rows.Count; )
            {
                this.dataGridViewRecord.Rows[i].DefaultCellStyle.BackColor = SystemColors.ActiveBorder;
                i += 2;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string keyword = string.Empty;
            List<Purchase> lstSearch = null;
            int selectIndex = cboSearchType.SelectedIndex;
            if(0 == selectIndex)
            {
                if (dataGridViewRecord.DataSource != lstPurchase)
                    dataGridViewRecord.DataSource = lstPurchase;
                return;
            }
            else if(1 == selectIndex)
            {
                keyword = this.dateTimePickerSearch.Value.ToString("yyyyMMdd");
                lstSearch= (from p in lstPurchase where p.PurchaseDate.ToString("yyyyMMdd").Equals(keyword) select p).ToList();
                //lstPurchase.FindAll();
            }else
            {
                keyword = this.txtKeyword.Text;
                lstSearch = (from p in lstPurchase where p.PurchaseProductName.Contains(keyword) select p).ToList();
            }
            dataGridViewRecord.DataSource = lstSearch;
        }

        private void cboSearchType_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = cboSearchType.SelectedIndex;
            if(index == 0)
            {
                this.txtKeyword.Enabled = false;
                this.dateTimePickerSearch.Enabled = false;
            }
            else if(index == 1)
            {
                this.txtKeyword.Enabled = false;
                this.dateTimePickerSearch.Enabled = true;
            }else
            {
                this.txtKeyword.Enabled = true;
                this.dateTimePickerSearch.Enabled = false;
            }
        }
    }
}
